//封装分类数据业务相关代码
import {getTopCategI} from '@/apis/category.js'
import {useRoute} from "vue-router";
import {ref,onMounted} from 'vue'
import { onBeforeRouteUpdate } from 'vue-router'

export function useCategory(){
    //获取数据
    const categoryData = ref({})
    const route = useRoute()

    const getCategory = async (id = route.params.id)=>{
        const res = await getTopCategI(id)
        categoryData.value = res.result
    }
    onMounted(()=>{getCategory()})


    onBeforeRouteUpdate((to)=>{
        getCategory(to.params.id)
    })

    return{
        categoryData
    }
}
